import Vue from 'vue'
import Vuex from 'vuex'
import {
	login,
	wxLogin,
	smsLogin,
	logout
} from '@/api/baseSys.js'

Vue.use(Vuex)

const BEARER = 'Bearer ';
export default new Vuex.Store({
	state: {
		token: '',
		userid: '',
		username: '',
		realname: '',
		welcome: '',
		avatar: ''
	},
	mutations: {
		SET_TOKEN: (state, token) => {
			state.token = token
		},
		SET_NAME: (state, {
			username,
			realname,
			userid,
			welcome
		}) => {
			state.username = username
			state.realname = realname
			state.welcome = welcome
		},
		SET_AVATAR: (state, avatar) => {
			state.avatar = avatar
		}
	},
	actions: {
		// 登录
		mLogin({
			commit
		}, userInfo) {
			return new Promise((resolve, reject) => {
				login(userInfo).then(response => {
					if (response.data.code == 200) {
						const result = response.data.data
						const userInfo = result.loginUser
						uni.setStorageSync('token', result.access_token);
						uni.setStorageSync('login_user_info', userInfo);
						commit('SET_TOKEN', result.access_token)
						commit('SET_NAME', {
							username: userInfo.name,
							realname: userInfo.nickName,
							userid: userInfo.socialUserId
						})
						resolve(response)
					} else {
						resolve(response)
					}
				}).catch(error => {
					console.log("catch===>response", response)
					reject(error)
				})
			})
		},
		//手机号登录
		PhoneLogin({
			commit
		}, userInfo) {
			return new Promise((resolve, reject) => {
				smsLogin(userInfo).then(response => {
					console.log(response);
					if (response.data.code == 200) {
						const result = response.data.data
						const userInfo = result.socialUser
						uni.setStorageSync('token', BEARER + result.access_token);
						uni.setStorageSync('login_user_info', userInfo);
						commit('SET_TOKEN', result.access_token)
						commit('SET_AVATAR', userInfo.ossId)
						commit('SET_NAME', {
							username: userInfo.name,
							realname: userInfo.nickName,
							userid: userInfo.socialUserId
						})
						resolve(response)
					} else if (response.data.code == 500) {
						uni.showToast({
							title: response.data.msg,
							icon: 'none'
						})
					} else {
						reject(response)
					}
				}).catch(error => {
					reject(error)
				})
			})
		},
		// 登出
		Logout({
			commit,
			state
		}) {
			return new Promise((resolve) => {
				let logoutToken = state.token;
				commit('SET_TOKEN', '');
				uni.removeStorageSync('token');
				uni.removeStorageSync('login_user_info');
				logout().then(() => {
					resolve();
				}).catch(() => {
					resolve()
				})
			})
		},

	},
	getters: {
		token: state => state.token,
		username: state => {
			return state.username || uni.getStorageSync('login_user_info').name;
		},
		realname: state => {
			return state.realname || uni.getStorageSync('login_user_info').nickName;
		},
		avatar: state => {
			return state.avatar || uni.getStorageSync('login_user_info').ossId;
		},
		userid: state => {
			//state.userid = uni.getStorageSync('login_user_info').socialUserId;
			return state.userid || uni.getStorageSync('login_user_info').userId;
		},
	}
})